package com.campus.common.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.campus.common.dto.UserDto;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
public class LoginUser implements UserDetails, Serializable {
    private static final long serialVersionUID = 1L;
    UserDto userDto;
    List<String> authorityList;
    private  List<GrantedAuthority> authorities;
    @JSONField(serialize = false)
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorityList!=null)
        {
//            authorities = authorityList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
            //  将权限封装并加入集合
            authorities = new ArrayList<GrantedAuthority>();
            for(String authority:authorityList)
            {
                System.out.println("authority: " + authority);
                authorities.add(new SimpleGrantedAuthority(authority));
            }
        }
        return authorities;
    }
    public LoginUser(UserDto user,List<String> authorityList)
    {
        this.userDto=user;
        this.authorityList=authorityList;
    }
    @Override
    public String getPassword() {
        return userDto.getPassword();
    }

    @Override
    public String getUsername() {
        return userDto.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
